<template>
  <div class="loginContainer">
    <div class="loginBox">
      <h3>登陆页面</h3>
      <el-form ref="loginForm" :rules="rules" :model="loginForm" label-width="80px">

        <el-form-item label="账号" prop="uname">
          <el-input placeholder="请输入用户名" prefix-icon="el-icon-user" v-model="loginForm.uname"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="psd">
          <el-input type="password" placeholder="请输入用密码" prefix-icon="iconfont icon-3702mima" v-model="loginForm.psd">
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="verify">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import axios from 'axios'
  export default {
    components: {},
    data() {
      return {
        loginForm: {
          uname: 'admin',
          psd: '123456'
        },
        rules: {
          uname: [{
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 6,
              message: '长度在 3 到 6个字符',
              trigger: 'blur'
            }
          ],
          psd: [{
              required: true,
              message: '请输入密码',
              trigger: 'change'
            },
            {
              min: 6,
              max: 16,
              message: '长度在 6 到 16 个字符',
              trigger: 'change'
            }
          ]
        }
      }
    },
    created() {},
    mounted() {},
    watch: {},
    computed: {},
    methods: {
      resetForm() {
        this.$refs.loginForm.resetFields()

      },
      verify() {
        this.$refs.loginForm.validate(val => {
          if (!val) {
            return this.$message.error('账号密码格式有误！');
          }
          let p = new Promise((resolve, resject) => {
            this.$axios.post('login', {
              username: this.loginForm.uname,
              password: this.loginForm.psd
            }).then(res => {
              resolve(res)
            })
          })
          p.then(val => {
            if (val.meta.status != 200) {
              return this.$message.error(val.meta.msg);
            }
            window.sessionStorage.setItem('token', val.data.token)
            this.$router.push('main')
          })

        })
      }
    },
  }

</script>
<style lang="css" scoped>
  .loginContainer {
    width: 100%;
    height: 100%;

    background-color: rgb(71, 71, 71);
  }

  .loginBox {
    width: 600px;
    background-color: #eee;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 40px;
    box-sizing: border-box;
  }

</style>
